package com.itestor.iptv.modules.alert.actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import com.itestor.iptv.global.actions.AbstractPagedQueryAction;
import com.itestor.iptv.global.bean.AbstractPagedQuery;
import com.itestor.iptv.modules.alert.bean.RobotLogQuery;
import com.itestor.iptv.modules.alert.service.RobotLogService;
import com.itestor.iptv.modules.alert.utils.AlertConstant;

/**
 * 
 * @author Mr.li
 * 
 */
public class RobotLogHistoryQueryAction extends AbstractPagedQueryAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(RobotLogHistoryQueryAction.class);
    
    private static SimpleDateFormat  sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.US);

    private RobotLogService robotLogService;

    @SuppressWarnings("unchecked")
    @Override
    public AbstractPagedQuery doPagedQuery(ActionForm form,
            HttpServletRequest request, int pagerCount, int pagerOffset)
            throws Exception {
        
        String robotName = (String) request.getParameter("robotName");
        String confirmStatus = (String) request.getParameter("confirmStatus");
        String startTime = (String) request.getParameter("startTime");
        String endTime = (String) request.getParameter("endTime");

        RobotLogQuery robotLogQuery = new RobotLogQuery();
        if (!StringUtils.isEmpty(robotName)) {
            robotLogQuery.setRobotName(robotName);
            request.setAttribute("robotName", robotName);
        }

        if (!StringUtils.isEmpty(confirmStatus)) {
            robotLogQuery.setConfirmStatus(Integer.parseInt(confirmStatus));
            request.setAttribute("confirmStatus", confirmStatus);
        }

        if (!StringUtils.isEmpty(startTime)) {
            Date start=sdf.parse(startTime);
            robotLogQuery.setStartLogTime(start.getTime()/1000);
            request.setAttribute("startTime", startTime);
        }
        
        if (!StringUtils.isEmpty(endTime)) {
            Date end=sdf.parse(endTime);
            robotLogQuery.setEndLogTime(end.getTime()/1000);            
            request.setAttribute("endTime", endTime);
        }        
     
        robotLogQuery.setPagerCount(pagerCount);
        robotLogQuery.setPagerOffset(pagerOffset);
                
        robotLogService.queryRobotLogList(robotLogQuery);
        
        request.setAttribute(AlertConstant.REQUEST_ATTR_Q_ROBOTLOG,
                robotLogQuery);
        
        return robotLogQuery;
    }

    /**
     * 
     * @param pRobotLogService
     */
    public void setRobotLogService(RobotLogService pRobotLogService) {
        robotLogService = pRobotLogService;
    }

}